<?php
class AccessAction extends LoginAction
{
    public function index()
    {
        $this->display();
    }

    /**
     * 角色信息显示
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function role()
    {
        $pid = intval($this->_get('pid'));
        $aid = intval($this->_get('aid'));
        $role = $this->getRole();
        if ($role && $this->isPost()) {
            $nodes = $this->_post('nodes');
            $nownodes = $this->_post('nownodes');
            if (count($nodes) >= 1) {
                $AccessModel = D('Access');
                $AccessModel->setAccess($role['id'], $nodes, $nownodes);
                $this->successMessage('允许设置成功！');
            } else {
                $this->errorMessage('必须选择一个以上节点！');
            }
        }
        //获取一级节点
        $NodeModel = D('Node');
        $nodes = $NodeModel->getNodes(0, 1);
        //获取二级标签信息
        if ($pid) {
            $twoNodes = $NodeModel->getNodes($pid, 2);
            if ($twoNodes) {
                $this->assign('pid', $pid);
                $this->checkNodesAccess(&$twoNodes, $role['id']);
                $this->assign('twoNodes', $twoNodes);
            }
        }
        //获取三级标签信息
        if ($aid) {
            $threeNodes = $NodeModel->getNodes($aid, 3);
            if ($threeNodes) {
                $this->checkNodesAccess(&$threeNodes, $role['id']);
                $this->assign('threeNodes', $threeNodes);
            }
        }
        //一级数据验证
        if ($nodes) {
            $this->checkNodesAccess(&$nodes, $role['id']);
        }
        //进行显示判断处理
        $this->assign('isTop', $pid ? false : true);
        $this->assign('isTwo', ($pid && !$aid) ? true : false);
        $this->assign('isThree', $aid ? true : false);
        $this->assign('nodes', $nodes);
        $this->display();
    }

    /**
     * 获取角色信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function getRole()
    {
        $id = intval($this->_get('id'));
        if (!$id) {
            $this->error('角色标识错误！');
        }
        $role = M('Role')->find($id);
        if (!$role) {
            $this->error('角色数据获取错误！');
        }
        $this->assign('id', $id);
        $this->assign('role', $role);
        return $role;
    }

    /**
     * 检测是否是已经允许角色访问
     * @param  $nodes
     * @param  $rid
     * @return void
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function checkNodesAccess(&$nodes, $rid)
    {
        $map = array();
        $AccessModel = M('Access');
        foreach ($nodes as $key => $node) {
            $map['role_id'] = $rid;
            $map['node_id'] = $node['id'];
            $map['level'] = $node['level'];
            $rs = $AccessModel->where($map)->find();
            if ($rs) {
                $nodes[$key]['isAccess'] = true;
            } else {
                $nodes[$key]['isAccess'] = false;
            }
        }
    }
}

?>